using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public class tbquydinhDAL
    {
        private SqlConnection cn = null;
        public tbquydinhDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbquydinhDAL(tbquydinhDAL quydinh)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbquydinh quydinh)
        {
            SqlCommand cm = new SqlCommand("quydinh_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@phihoantienbanggold", quydinh.Phihoantienbanggold);
            cm.Parameters.AddWithValue("@phihoantienbangtienmat", quydinh.Phihoantienbangtienmat);
            cm.Parameters.AddWithValue("@thoigiangiaohangtoithieu", quydinh.Thoigiangiaohangtoithieu);
            cm.Parameters.AddWithValue("@thoigiangiaohangtoida", quydinh.Thoigiangiaohangtoida);
            cm.Parameters.AddWithValue("@doigoldthanhtien", quydinh.Doigoldthanhtien);
            cm.Parameters.AddWithValue("@sotiennaptoithieu", quydinh.Sotiennaptoithieu);
            cm.ExecuteNonQuery();
        }
        public void Update(tbquydinh quydinh)
        {
            SqlCommand cm = new SqlCommand("quydinh_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@slphieutoidachomotsanpham", quydinh.Slphieutoithieuchomotsanpham);
            cm.Parameters.AddWithValue("@phihoantienbanggold", quydinh.Phihoantienbanggold);
            cm.Parameters.AddWithValue("@phihoantienbangtienmat", quydinh.Phihoantienbangtienmat);
            cm.Parameters.AddWithValue("@thoigiangiaohangtoithieu", quydinh.Thoigiangiaohangtoithieu);
            cm.Parameters.AddWithValue("@thoigiangiaohangtoida", quydinh.Thoigiangiaohangtoida);
            cm.Parameters.AddWithValue("@doigoldthanhtien", quydinh.Doigoldthanhtien);
            cm.Parameters.AddWithValue("@sotiennaptoithieu", quydinh.Sotiennaptoithieu);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("quydinh_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@slphieutoidachomotsanpham", iD);
            cm.ExecuteNonQuery();
        }
        public tbquydinhcollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("quydinh_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbquydinhcollection quydinh1Collection = new tbquydinhcollection();
            while (dr.Read())
            {
                tbquydinh quydinh = new tbquydinh();
                quydinh.Slphieutoithieuchomotsanpham= int.Parse(dr.GetValue(0).ToString());
                quydinh.Phihoantienbanggold = int.Parse(dr.GetValue(1).ToString());
                quydinh.Phihoantienbangtienmat = int.Parse(dr.GetValue(2).ToString());
                quydinh.Thoigiangiaohangtoithieu = int.Parse(dr.GetValue(3).ToString());
                quydinh.Thoigiangiaohangtoida = int.Parse(dr.GetValue(4).ToString());
                quydinh.Doigoldthanhtien = int.Parse(dr.GetValue(5).ToString());
                quydinh.Sotiennaptoithieu = int.Parse(dr.GetValue(6).ToString());
                quydinh1Collection.Add(quydinh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return quydinh1Collection;
        }
    }
}
